package com.csgc.camera.biz.other.activity

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import coil.load

import com.csgc.camera.base.BaseBindingActivity

import com.csgc.camera.events.PuzzleSaveEvents
import com.csgc.camera.util.CommonUtils
import com.csgc.camera.util.ViewUnit
import com.csgc.camera.util.ViewUtils
import com.csgc.camera.util.clickWithTrigger
import com.csgc.camera.util.dp
import com.csgc.camera.util.saveToAlbum
import com.csgc.camera.widget.dialog.LoadingPop
import com.cysd.spsyxj.R
import com.cysd.spsyxj.databinding.ActivityPuzzleBinding

import com.drake.channel.sendEvent
import com.elvishew.xlog.XLog
import com.gyf.immersionbar.ktx.immersionBar
import com.hjq.toast.ToastUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File


class PuzzleActivity:BaseBindingActivity<ActivityPuzzleBinding>(ActivityPuzzleBinding::inflate) {


    companion object{
        private const val KEY_PATH_LIST = "pathList"
        private const val KEY_STYLE = "style"
        private const val KEY_RATIO_WIDTH = "ratio_width"
        private const val KEY_RATIO_HEIGHT = "ratio_height"
        fun launch(context: Context, list:ArrayList<String>,style:Int,width:Int,height:Int){
            val intent = Intent(context,PuzzleActivity::class.java)
            intent.putStringArrayListExtra(KEY_PATH_LIST,list)
            intent.putExtra(KEY_STYLE,style)
            intent.putExtra(KEY_RATIO_WIDTH,width)
            intent.putExtra(KEY_RATIO_HEIGHT,height)
            context.startActivity(intent)
        }

    }

    private val pathList:ArrayList<String> by lazy {
        intent.getStringArrayListExtra(KEY_PATH_LIST)?:ArrayList()
    }
    private val style:Int by lazy {
        intent.getIntExtra(KEY_STYLE,1)
    }
    private val ratioWidth:Int by lazy {
        intent.getIntExtra(KEY_RATIO_WIDTH,9)
    }
    private val ratioHeight:Int by lazy {
        intent.getIntExtra(KEY_RATIO_HEIGHT,16)
    }
    private val popLoading:LoadingPop by lazy {
        LoadingPop(this,true)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        immersionBar {
            fitsSystemWindows(true)
            statusBarColor(R.color.black)
            statusBarDarkFont(false)
        }

        binding.ivBack.clickWithTrigger {
            finish()
        }

        if(style == 1){
            binding.layoutStyle1.visibility = View.VISIBLE
            initStyle1()
        }else if(style == 2){
            binding.layoutStyle2.visibility = View.VISIBLE
            initStyle2()
        }else if(style == 3){
            binding.layoutStyle3.visibility = View.VISIBLE
            initStyle3()
        }else if (style == 4){
            binding.layoutStyle4.visibility = View.VISIBLE
            initStyle4()
        }



        binding.stvSavePuzzle.clickWithTrigger {
            var bitmap:Bitmap?=null
            if(style == 1){
                 bitmap = ViewUnit.captureView(binding.layoutStyle1)

            }else if(style == 2){
                 bitmap = ViewUnit.captureView(binding.layoutStyle2)

            }else if(style == 3){
                 bitmap = ViewUnit.captureView(binding.layoutStyle3)

            }else if(style == 4){
                 bitmap = ViewUnit.captureView(binding.layoutStyle4)

            }
            val name = "today_puzzle_${System.currentTimeMillis()}.png"

            popLoading.showLoading()
            lifecycleScope.launch(Dispatchers.IO){

                val uri =  bitmap?.saveToAlbum(this@PuzzleActivity,name, quality = 100)
                lifecycleScope.launch(Dispatchers.Main) {
                    if(uri !=null){
                        ToastUtils.show("已保存至相册！")
                    }
                    popLoading.dismissLoading()

                    pathList.forEach {
                        File(it).delete()
                    }
                    sendEvent(PuzzleSaveEvents(true))
                    finish()
                }



            }


        }

    }



    private fun initStyle1(){

        var imgWidth = 0
        var imgHeight = 0

        if (pathList.size == 1){

            imgWidth = ViewUtils.getScreenWidth(this)
            imgHeight = imgWidth*ratioHeight/ratioWidth

            val parentParams = binding.layoutStyle1.layoutParams
            parentParams.width = imgWidth
            parentParams.height = imgHeight
            binding.layoutStyle1.layoutParams = parentParams

            val params = binding.ivPuzzle31.layoutParams
            params.width = imgWidth
            params.height = imgHeight
            binding.ivPuzzle31.layoutParams = params

            binding.ivPuzzle32.visibility = View.GONE
            binding.ivPuzzle33.visibility = View.GONE
            binding.space31.visibility = View.GONE
            binding.space32.visibility = View.GONE

        }else if (pathList.size == 2){
            imgHeight = (ViewUtils.getScreenHeight(this) - ViewUtils.dp2px(2f)) /2
            imgWidth = imgHeight*ratioWidth/ratioHeight

            val parentParams =  binding.layoutStyle1.layoutParams
            parentParams.width = imgWidth
            binding.layoutStyle1.layoutParams = parentParams

            val params = binding.ivPuzzle31.layoutParams
            params.width = imgWidth
            params.height = imgHeight
            binding.ivPuzzle31.layoutParams = params

            val params2 = binding.ivPuzzle32.layoutParams
            params2.width = imgWidth
            params2.height = imgHeight
            binding.ivPuzzle32.layoutParams = params2


            binding.space32.visibility = View.GONE
            binding.ivPuzzle33.visibility = View.GONE
        }else if (pathList.size == 3){
            imgHeight = (ViewUtils.getScreenHeight(this) - ViewUtils.dp2px(4f)) /2
            imgWidth = imgHeight*ratioWidth/ratioHeight

           val parentParams =  binding.layoutStyle1.layoutParams
            parentParams.width = imgWidth
            binding.layoutStyle1.layoutParams = parentParams

            val params = binding.ivPuzzle31.layoutParams
            params.width = imgWidth
            params.height = imgHeight
            binding.ivPuzzle31.layoutParams = params

            val params2 = binding.ivPuzzle32.layoutParams
            params2.width = imgWidth
            params2.height = imgHeight
            binding.ivPuzzle32.layoutParams = params2

            val params3 = binding.ivPuzzle33.layoutParams
            params3.width = imgWidth
            params3.height = imgHeight
            binding.ivPuzzle33.layoutParams = params3
        }

        pathList.forEachIndexed { index, s ->
            when(index){
                0->{binding.ivPuzzle31.load(s){allowHardware(false)}}
                1->{binding.ivPuzzle32.load(s){allowHardware(false)}}
                2->{binding.ivPuzzle33.load(s){allowHardware(false)}}
            }
        }

    }

    private fun initStyle2(){
        var imgWidth = 0
        var imgHeight = 0

        if (pathList.size == 1){
            imgWidth = ViewUtils.getScreenWidth(this)
            imgHeight = imgWidth*ratioHeight/ratioWidth
            val params = binding.ivPuzzle41.layoutParams
            params.width = imgWidth
            params.height = imgHeight
            binding.ivPuzzle41.layoutParams = params

            binding.space41.visibility = View.GONE
            binding.space42.visibility = View.GONE

        }else if(pathList.size == 2){
            imgWidth = ViewUtils.getScreenWidth(this) -ViewUtils.dp2px(2f)
            imgHeight = (ViewUtils.getScreenWidth(this)-ViewUtils.dp2px(2f))/2*ratioHeight/ratioWidth
            val parentParams =  binding.layoutStyle2.layoutParams
            parentParams.width = imgWidth
            parentParams.height = imgHeight
            binding.layoutStyle2.layoutParams = parentParams

            /*val params1 = binding.ivPuzzle41.layoutParams
            params1.width = imgWidth/2
            params1.height = imgHeight
            binding.ivPuzzle41.layoutParams = params1


            val params2 =  binding.ivPuzzle42.layoutParams
            params2.width = imgWidth/2
            params2.height = imgHeight
            binding.ivPuzzle42.layoutParams = params2*/



            binding.space42.visibility = View.GONE
            binding.ivPuzzle43.visibility = View.GONE
            binding.ivPuzzle44.visibility = View.GONE

        }else if (pathList.size>2){
            imgWidth = (ViewUtils.getScreenWidth(this) - ViewUtils.dp2px(2f))/2
            imgHeight = imgWidth*ratioHeight/ratioWidth


            val parentParams =  binding.layoutStyle2.layoutParams
            parentParams.width = ViewUtils.getScreenWidth(this)
            parentParams.height = imgHeight*2
            binding.layoutStyle2.layoutParams = parentParams



            val params1 = binding.ivPuzzle41.layoutParams
            params1.width = imgWidth
            params1.height = imgHeight
            binding.ivPuzzle41.layoutParams = params1


            val params2 =  binding.ivPuzzle42.layoutParams
            params2.width = imgWidth
            params2.height = imgHeight
            binding.ivPuzzle42.layoutParams = params2

            val params3 = binding.ivPuzzle43.layoutParams
            params3.width = imgWidth
            params3.height = imgHeight
            binding.ivPuzzle43.layoutParams = params3

            val params4 = binding.ivPuzzle44.layoutParams
            params4.width = imgWidth
            params4.height = imgHeight
            binding.ivPuzzle44.layoutParams = params4


        }

        pathList.forEachIndexed { index, s ->

            when(index){
                0->{binding.ivPuzzle41.load(s){allowHardware(false)}}
                1->{binding.ivPuzzle42.load(s){allowHardware(false)}}
                2->{binding.ivPuzzle43.load(s){allowHardware(false)}}
                3->{binding.ivPuzzle44.load(s){allowHardware(false)}}
            }
        }

    }

    private fun initStyle3(){
        var imgWidth = 0
        var imgHeight = 0
        if (pathList.size == 1){
            imgWidth = ViewUtils.getScreenWidth(this)
            imgHeight = imgWidth*ratioHeight/ratioWidth

            val parentParams = binding.layoutStyle3.layoutParams
            parentParams.width = imgWidth
            parentParams.height = imgHeight
            binding.layoutStyle3.layoutParams = parentParams


            val params = binding.ivPuzzle61.layoutParams
            params.width = imgWidth
            params.height = imgHeight
            binding.ivPuzzle61.layoutParams = params

            binding.space61.visibility = View.GONE

        }else if(pathList.size == 2){

            imgWidth = (ViewUtils.getScreenWidth(this) - ViewUtils.dp2px(2f))/2
            imgHeight = imgWidth *ratioHeight/ratioWidth

            val parentParams = binding.layoutStyle3.layoutParams
            parentParams.width = ViewUtils.getScreenWidth(this)
            parentParams.height = imgHeight


            val params = binding.ivPuzzle61.layoutParams
            params.width = imgWidth
            params.height = imgHeight
            binding.ivPuzzle61.layoutParams = params

            val params2 = binding.ivPuzzle62.layoutParams
            params2.width = imgWidth
            params2.height = imgHeight
            binding.ivPuzzle62.layoutParams = params2




            binding.ivPuzzle63.visibility = View.GONE
            binding.ivPuzzle64.visibility = View.GONE
            binding.ivPuzzle65.visibility = View.GONE
            binding.ivPuzzle66.visibility = View.GONE
            binding.space62.visibility = View.GONE
            binding.space63.visibility = View.GONE

        }else if (pathList.size == 3 || pathList.size == 4){

            imgWidth = (ViewUtils.getScreenWidth(this) - ViewUtils.dp2px(2f))/2
            imgHeight = imgWidth*ratioHeight/ratioWidth
            val params1 = binding.ivPuzzle61.layoutParams
            params1.width = imgWidth
            params1.height = imgHeight
            binding.ivPuzzle61.layoutParams = params1


           val params2 =  binding.ivPuzzle62.layoutParams
            params2.width = imgWidth
            params2.height = imgHeight
            binding.ivPuzzle62.layoutParams = params2

            val params3 = binding.ivPuzzle63.layoutParams
            params3.width = imgWidth
            params3.height = imgHeight
            binding.ivPuzzle63.layoutParams = params3

            val params4 = binding.ivPuzzle64.layoutParams
            params4.width = imgWidth
            params4.height = imgHeight
            binding.ivPuzzle64.layoutParams = params4


            binding.ivPuzzle65.visibility = View.GONE
            binding.ivPuzzle66.visibility = View.GONE
            binding.space63.visibility = View.GONE
        }else if(pathList.size>4){
            imgHeight = (ViewUtils.getScreenHeight(this)  - ViewUtils.dp2px(4f)) /3
            imgWidth = imgHeight*ratioWidth/ratioHeight

           val parentParams =  binding.layoutStyle3.layoutParams
            parentParams.width = imgWidth*2 + ViewUtils.dp2px(2f)
            parentParams.height = imgHeight*3 + ViewUtils.dp2px(4f)
            binding.layoutStyle3.layoutParams = parentParams

            val params1 = binding.ivPuzzle61.layoutParams
            params1.width = imgWidth
            params1.height = imgHeight
            binding.ivPuzzle61.layoutParams = params1


            val params2 =  binding.ivPuzzle62.layoutParams
            params2.width = imgWidth
            params2.height = imgHeight
            binding.ivPuzzle62.layoutParams = params2

            val params3 = binding.ivPuzzle63.layoutParams
            params3.width = imgWidth
            params3.height = imgHeight
            binding.ivPuzzle63.layoutParams = params3

            val params4 = binding.ivPuzzle64.layoutParams
            params4.width = imgWidth
            params4.height = imgHeight
            binding.ivPuzzle64.layoutParams = params4


            val params5 = binding.ivPuzzle65.layoutParams
            params5.width = imgWidth
            params5.height = imgHeight
            binding.ivPuzzle65.layoutParams = params5

            val params6 = binding.ivPuzzle66.layoutParams
            params6.width = imgWidth
            params6.height = imgHeight
            binding.ivPuzzle66.layoutParams = params6
        }

        pathList.forEachIndexed { index, s ->
            when(index){
                0->{binding.ivPuzzle61.load(s){allowHardware(false)}}
                1->{binding.ivPuzzle62.load(s){allowHardware(false)}}
                2->{binding.ivPuzzle63.load(s){allowHardware(false)}}
                3->{binding.ivPuzzle64.load(s){allowHardware(false)}}
                4->{binding.ivPuzzle65.load(s){allowHardware(false)}}
                5->{binding.ivPuzzle66.load(s){allowHardware(false)}}
            }
        }

    }

    private fun initStyle4(){

        var imgWidth = 0
        var imgHeight = 0
        if (pathList.size == 1){
            imgWidth = ViewUtils.getScreenWidth(this)
            imgHeight = imgWidth*ratioHeight/ratioWidth

            val parentParams = binding.layoutStyle4.layoutParams
            parentParams.width = imgWidth
            parentParams.height = imgHeight
            binding.layoutStyle4.layoutParams = parentParams

            val params = binding.ivPuzzle91.layoutParams
            params.width = imgWidth
            params.height = imgHeight
            binding.ivPuzzle91.layoutParams = params



        }else if(pathList.size == 2){
            imgWidth = (ViewUtils.getScreenWidth(this) - ViewUtils.dp2px(2f))/2
            imgHeight = imgWidth*ratioHeight/ratioWidth

            val parentParams = binding.layoutStyle4.layoutParams
            parentParams.width = imgWidth*2 + ViewUtils.dp2px(2f)
            parentParams.height = imgHeight
            binding.layoutStyle4.layoutParams = parentParams

            val params1 = binding.ivPuzzle91.layoutParams
            params1.width = imgWidth
            params1.height = imgHeight
            binding.ivPuzzle91.layoutParams = params1

            val params2 = binding.ivPuzzle92.layoutParams
            params2.width = imgWidth
            params2.height = imgHeight
            binding.ivPuzzle92.layoutParams = params2


        }else if(pathList.size == 3){
            imgWidth = (ViewUtils.getScreenWidth(this) - ViewUtils.dp2px(4f))/3
            imgHeight = imgWidth*ratioHeight/ratioWidth

            val parentParams = binding.layoutStyle4.layoutParams
            parentParams.width = imgWidth*3 + ViewUtils.dp2px(4f)
            parentParams.height = imgHeight
            binding.layoutStyle4.layoutParams = parentParams

            val params1 = binding.ivPuzzle91.layoutParams
            params1.width = imgWidth
            params1.height = imgHeight
            binding.ivPuzzle91.layoutParams = params1

            val params2 = binding.ivPuzzle92.layoutParams
            params2.width = imgWidth
            params2.height = imgHeight
            binding.ivPuzzle92.layoutParams = params2

            val params3 = binding.ivPuzzle93.layoutParams
            params3.width = imgWidth
            params3.height = imgHeight
            binding.ivPuzzle93.layoutParams = params3


        }else if (pathList.size in 4..6){
            imgWidth = (ViewUtils.getScreenWidth(this) - ViewUtils.dp2px(4f))/3
            imgHeight = imgWidth*ratioHeight/ratioWidth

            val parentParams = binding.layoutStyle4.layoutParams
            parentParams.width = imgWidth*3 + ViewUtils.dp2px(4f)
            parentParams.height = imgHeight*2 + ViewUtils.dp2px(2f)
            binding.layoutStyle4.layoutParams = parentParams

            val params1 = binding.ivPuzzle91.layoutParams
            params1.width = imgWidth
            params1.height = imgHeight
            binding.ivPuzzle91.layoutParams = params1

            val params2 = binding.ivPuzzle92.layoutParams
            params2.width = imgWidth
            params2.height = imgHeight
            binding.ivPuzzle92.layoutParams = params2

            val params3 = binding.ivPuzzle93.layoutParams
            params3.width = imgWidth
            params3.height = imgHeight
            binding.ivPuzzle93.layoutParams = params3

            val params4 = binding.ivPuzzle94.layoutParams
            params4.width = imgWidth
            params4.height = imgHeight
            binding.ivPuzzle94.layoutParams = params4

            val params5 = binding.ivPuzzle95.layoutParams
            params5.width = imgWidth
            params5.height = imgHeight
            binding.ivPuzzle95.layoutParams = params5

            val params6 = binding.ivPuzzle96.layoutParams
            params6.width = imgWidth
            params6.height = imgHeight
            binding.ivPuzzle96.layoutParams = params6
        }else{
            imgWidth = (ViewUtils.getScreenWidth(this) - ViewUtils.dp2px(4f))/3
            imgHeight = imgWidth*ratioHeight/ratioWidth

            val params1 = binding.ivPuzzle91.layoutParams
            params1.width = imgWidth
            params1.height = imgHeight
            binding.ivPuzzle91.layoutParams = params1

            val params2 = binding.ivPuzzle92.layoutParams
            params2.width = imgWidth
            params2.height = imgHeight
            binding.ivPuzzle92.layoutParams = params2

            val params3 = binding.ivPuzzle93.layoutParams
            params3.width = imgWidth
            params3.height = imgHeight
            binding.ivPuzzle93.layoutParams = params3

            val params4 = binding.ivPuzzle94.layoutParams
            params4.width = imgWidth
            params4.height = imgHeight
            binding.ivPuzzle94.layoutParams = params4

            val params5 = binding.ivPuzzle95.layoutParams
            params5.width = imgWidth
            params5.height = imgHeight
            binding.ivPuzzle95.layoutParams = params5

            val params6 = binding.ivPuzzle96.layoutParams
            params6.width = imgWidth
            params6.height = imgHeight
            binding.ivPuzzle96.layoutParams = params6

            val params7 = binding.ivPuzzle97.layoutParams
            params7.width = imgWidth
            params7.height = imgHeight
            binding.ivPuzzle97.layoutParams = params7

            val params8 = binding.ivPuzzle98.layoutParams
            params8.width = imgWidth
            params8.height = imgHeight
            binding.ivPuzzle98.layoutParams = params8

            val params9 = binding.ivPuzzle99.layoutParams
            params9.width = imgWidth
            params9.height = imgHeight
            binding.ivPuzzle99.layoutParams = params9
        }




        pathList.forEachIndexed { index, s ->
            when(index){
                0->{binding.ivPuzzle91.load(s){allowHardware(false)}}
                1->{binding.ivPuzzle92.load(s){allowHardware(false)}}
                2->{binding.ivPuzzle93.load(s){allowHardware(false)}}
                3->{binding.ivPuzzle94.load(s){allowHardware(false)}}
                4->{binding.ivPuzzle95.load(s){allowHardware(false)}}
                5->{binding.ivPuzzle96.load(s){allowHardware(false)}}
                6->{binding.ivPuzzle97.load(s){allowHardware(false)}}
                7->{binding.ivPuzzle98.load(s){allowHardware(false)}}
                8->{binding.ivPuzzle99.load(s){allowHardware(false)}}
            }
        }

    }



}